﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;

namespace BabyShop.WebSite.Utility
{
    public class ActionsHelper
    {
        public IList<string> GetAllAreas(string areaPath)
        {
            var areaRootDir = new DirectoryInfo(areaPath);
            var areaDirs = areaRootDir.GetDirectories();

            return areaDirs.Select(p => p.Name).ToList();
        }

        public IList<string> GetAllControllers(string nameSpaceFormat, string areaName)
        {
            string assemblyName = string.Format(nameSpaceFormat, areaName);
            var assemblyTypes = Assembly.Load(assemblyName).GetTypes();
            IList<string> controllerNameList = assemblyTypes
                .Where(p => p.IsSubclassOf(typeof (Controller)) && p.Name != "BaseController")
                .Select(p => p.FullName).ToList();

            return controllerNameList;
        }

        public IList<string> GetAllActions(string controllerName)
        {
            Type controllerType = Assembly.Load(controllerName).GetType();
            IList<string> actionNameList = controllerType.GetMethods()
                .Where(p => !p.IsDefined(typeof (HttpPostAttribute), false)
                            && !p.IsDefined(typeof (NonActionAttribute), false)).Select(p => p.Name).ToList();

            return actionNameList;
        }
    }
}